package com.example.lt.mytest;

import android.media.MediaPlayer;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.AndroidCharacter;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SeekBar;

import java.io.File;
import java.util.ArrayList;

public class MusicActivity extends AppCompatActivity {
    private MediaPlayer player;
    private String path;
    private ListView songslv;
    private String curreatSong;
    private String[] fileList;
    private SeekBar seekBar ;
    private boolean statOrPuase = true;
    private int musicLong;

    private void getAllSongs(){
        path = Environment.getExternalStorageDirectory().getPath()+"/mp3/";
        fileList =new File(path).list();


    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music);
        seekBar = (SeekBar) findViewById(R.id.seekBar);

        songslv= (ListView) findViewById(R.id.songslv);



        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,fileList);
        songslv.setAdapter(adapter);
        player = new MediaPlayer();
        songslv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                curreatSong = path+"/"+fileList[position];
            }
        });

        //seekbar设置监听
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            private int progress;
            /*
            * seekbar改变时的事件监听处理
            * */
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                Log.i("LT","当前进度："+progress+"%");
                this.progress = progress;
//                player.seekTo(progress);
//                Log.d("debug",String.valueOf(seekBar.getId()));
            }
            /*
            * 按住seekbar时的事件监听处理
            * */
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {


            }
            /*
            * 放开seekbar时的时间监听处理
            * */
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                player.seekTo(progress*1000);

            }
        });

    }

    private void init() {
        new Thread(new Runnable(){
            @Override
            public void run() {
                while (true){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
//                    double a =((double)player.getCurrentPosition() / musicLong);
//                    Log.i("1",a+"");
//                    int progress = (int) a*100;
//                    Log.i("1",progress+"");
                    seekBar.setProgress(player.getCurrentPosition()/1000);


                }

            }
        }).start();

    }

    public void play(View view){
        try {
            player.reset();
            player.setDataSource(curreatSong);

            player.prepare();
            player.start();
            musicLong = player.getDuration();
//            Log.i("musicLong",musicLong+"");
            init();
        }catch (Exception e){

        }

    }

    public void puase(View view) {
        if (statOrPuase){
            player.pause();
            statOrPuase = false;
        }else {
            player.start();
            statOrPuase = true;
        }

    }
}
